package com.gitee.feizns.web.ex.impl;

import com.gitee.feizns.web.domain.result.Ret;
import com.gitee.feizns.web.ex.CustomizeExceptionHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;

/**
 * @author feizns
 * @since 2019/7/7
 */
public class HttpMessageNotReadableExceptionHandler implements CustomizeExceptionHandler<HttpMessageNotReadableException, Object> {

    private static final String MSG = "Required request body is missing";

    @Override
    public ResponseEntity<Ret<Object>> handle(HttpMessageNotReadableException e) {
        String message = e.getMessage();
        if ( message != null && message.startsWith(MSG) )
            message = MSG;
        return ResponseEntity.status(HttpStatus.BAD_REQUEST.value())
                .body(Ret.err(HttpStatus.BAD_REQUEST.value(), message));
    }

}
